home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / bfd.mips / aoutx.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  47.8 KB  |  1,618 lines

  1. /* BFD semi-generic back-end for a.out binaries
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*doc*
  22. @section a.out backends
  23.  
  24. BFD supports a number of different flavours of a.out format, though
  25. the major differences are only the sizes of the structures on disk,
  26. and the shape of the relocation information. 
  27.  
  28. The support is split into a basic support file @code{aoutx.h} and
  29. other files which derive functions from the base. One derivation file
  30. is @code{aoutf1.h} (for a.out flavour 1), and adds to the basic a.out
  31. functions support for sun3, sun4, 386 and 29k a.out files, to create a
  32. target jump vector for a specific target.
  33.  
  34. This information is further split out into more specific files for each
  35. machine, including @code{sunos.c} - for sun3 and sun4 and
  36. @code{demo64} for a demonstration of a 64 bit a.out format.
  37.  
  38. The base file @code{aoutx.h} defines general mechanisms for reading
  39. and writing records to and from disk, and various other methods which
  40. bfd requires. It is included by @code{aout32.c} and @code{aout64.c} to
  41. form the names aout_32_swap_exec_header_in,
  42. aout_64_swap_exec_header_in, etc.
  43.  
  44. As an example, this is what goes on to make the back end for a sun4, from aout32.c
  45.  
  46. @example
  47.    #define ARCH_SIZE 32
  48.    #include "aoutx.h"
  49. @end example
  50.  
  51. Which exports names:
  52. @example
  53.     ...
  54.    aout_32_canonicalize_reloc
  55.    aout_32_find_nearest_line
  56.    aout_32_get_lineno
  57.    aout_32_get_reloc_upper_bound
  58.      ...
  59. @end example
  60.  
  61. from sunos.c
  62.  
  63. @example   
  64.     #define ARCH 32
  65.     #define TARGET_NAME "a.out-sunos-big"
  66.     #define VECNAME    sunos_big_vec
  67.     #include "aoutf1.h"
  68. @end example
  69. requires all the names from aout32.c, and produces the jump vector
  70.  
  71. @example
  72.     sunos_big_vec
  73. @end example
  74.  
  75. */
  76.  
  77.  
  78. #include "bfd.h"
  79. #include <sysdep.h>
  80. #include <ansidecl.h>
  81.  
  82. struct external_exec;
  83. #include "libaout.h"
  84. #include "libbfd.h"
  85. #include "aout64.h"
  86. #include "stab.gnu.h"
  87. #include "ar.h"
  88.  
  89. void (*bfd_error_trap)();
  90.  
  91. /*doc*
  92. @subsection relocations
  93. The file @code{aoutx.h} caters for both the @emph{standard} and
  94. @emph{extended} forms of a.out relocation records.
  95.  
  96. The standard records are characterised by containing only an address,
  97. a symbol index and a type field. The extended records (used on 29ks
  98. and sparcs) also have a full integer for an addend. 
  99. */
  100. #define CTOR_TABLE_RELOC_IDX 2
  101.  
  102.  
  103. static  reloc_howto_type howto_table_ext[] = 
  104. {
  105.   HOWTO(RELOC_8,      0,  0,      8,  false, 0, true,  true,0,"8",      false, 0,0x000000ff, false),
  106.   HOWTO(RELOC_16,     0,  1,     16, false, 0, true,  true,0,"16",      false, 0,0x0000ffff, false),
  107.   HOWTO(RELOC_32,     0,  2,     32, false, 0, true,  true,0,"32",      false, 0,0xffffffff, false),
  108.   HOWTO(RELOC_DISP8,  0,  0,     8,  true,  0, false, true,0,"DISP8",     false, 0,0x000000ff, false),
  109.   HOWTO(RELOC_DISP16, 0,  1,     16, true,  0, false, true,0,"DISP16",     false, 0,0x0000ffff, false),
  110.   HOWTO(RELOC_DISP32, 0,  2,     32, true,  0, false, true,0,"DISP32",     false, 0,0xffffffff, false),
  111.   HOWTO(RELOC_WDISP30,2,  2,     30, true,  0, false, true,0,"WDISP30",     false, 0,0x3fffffff, false),
  112.   HOWTO(RELOC_WDISP22,2,  2,     22, true,  0, false, true,0,"WDISP22",     false, 0,0x003fffff, false),
  113.   HOWTO(RELOC_HI22,   10, 2,     22, false, 0, false, true,0,"HI22",    false, 0,0x003fffff, false),
  114.   HOWTO(RELOC_22,      0, 2,     22, false, 0, false, true,0,"22",       false, 0,0x003fffff, false),
  115.   HOWTO(RELOC_13,     0, 2,     13, false, 0, false, true,0,"13",       false, 0,0x00001fff, false),
  116.   HOWTO(RELOC_LO10,     0, 2,     10, false, 0, false, true,0,"LO10",     false, 0,0x000003ff, false),
  117.   HOWTO(RELOC_SFA_BASE,0, 2,     32, false, 0, false, true,0,"SFA_BASE", false, 0,0xffffffff, false),
  118.   HOWTO(RELOC_SFA_OFF13,0,2,     32, false, 0, false, true,0,"SFA_OFF13",false, 0,0xffffffff, false),
  119.   HOWTO(RELOC_BASE10, 0,  2,     16, false, 0, false, true,0,"BASE10",   false, 0,0x0000ffff, false),
  120.   HOWTO(RELOC_BASE13, 0,  2,    13, false, 0, false, true,0,"BASE13",   false, 0,0x00001fff, false),
  121.   HOWTO(RELOC_BASE22, 0,  2,    0,  false, 0, false, true,0,"BASE22",   false, 0,0x00000000, false),
  122.   HOWTO(RELOC_PC10,   0,  2,    10, false, 0, false, true,0,"PC10",    false, 0,0x000003ff, false),
  123.   HOWTO(RELOC_PC22,   0,  2,    22, false, 0, false, true,0,"PC22",    false, 0,0x003fffff, false),
  124.   HOWTO(RELOC_JMP_TBL,0,  2,    32, false, 0, false, true,0,"JMP_TBL",    false, 0,0xffffffff, false),
  125.   HOWTO(RELOC_SEGOFF16,0, 2,    0,  false, 0, false, true,0,"SEGOFF16",    false, 0,0x00000000, false),
  126.   HOWTO(RELOC_GLOB_DAT,0, 2,    0,  false, 0, false, true,0,"GLOB_DAT",    false, 0,0x00000000, false),
  127.   HOWTO(RELOC_JMP_SLOT,0, 2,    0,  false, 0, false, true,0,"JMP_SLOT",    false, 0,0x00000000, false),
  128.   HOWTO(RELOC_RELATIVE,0, 2,    0,  false, 0, false,    true,0,"RELATIVE",    false, 0,0x00000000, false),
  129.  
  130. };
  131.  
  132. /* Convert standard reloc records to "arelent" format (incl byte swap).  */
  133.  
  134. static  reloc_howto_type howto_table_std[] = {
  135.   /* type           rs   size bsz  pcrel bitpos  abs ovrf sf name    part_inpl   readmask  setmask  pcdone */
  136. HOWTO( 0,           0,  0,      8,  false, 0, true,  true,0,"8",    true, 0x000000ff,0x000000ff, false),
  137. HOWTO( 1,           0,  1,     16, false, 0, true,  true,0,"16",    true, 0x0000ffff,0x0000ffff, false),
  138. HOWTO( 2,           0,  2,     32, false, 0, true,  true,0,"32",    true, 0xffffffff,0xffffffff, false),
  139. HOWTO( 3,           0,  3,     64, false, 0, true,  true,0,"64",       true, 0xdeaddead,0xdeaddead, false),
  140. HOWTO( 4,           0,  0,     8,  true,  0, false, true,0,"DISP8",    true, 0x000000ff,0x000000ff, false),
  141. HOWTO( 5,           0,  1,     16, true,  0, false, true,0,"DISP16",   true, 0x0000ffff,0x0000ffff, false),
  142. HOWTO( 6,           0,  2,     32, true,  0, false, true,0,"DISP32",   true, 0xffffffff,0xffffffff, false),
  143. HOWTO( 7,           0,  3,     64, true,  0, false, true,0,"DISP64",   true, 0xfeedface,0xfeedface, false),
  144. };
  145.  
  146.  
  147. bfd_error_vector_type bfd_error_vector;
  148.  
  149. /*doc*
  150. @subsection Internal Entry Points
  151. @code{aoutx.h} exports several routines for accessing the contents of
  152. an a.out file, which are gathered and exported in turn by various
  153. format specific files (eg sunos.c).
  154. */
  155.  
  156. /*doc*
  157. *i aout_<size>_swap_exec_header_in
  158. Swaps the information in an executable header taken from a raw byte stream memory image,
  159. into the internal exec_header structure.
  160. *; PROTO(void, aout_<size>_swap_exec_header_in,
  161.       (bfd *abfd,
  162.       struct external_exec *raw_bytes,
  163.       struct internal_exec *execp));
  164. */
  165.      
  166. void
  167. DEFUN(NAME(aout,swap_exec_header_in),(abfd, raw_bytes, execp),
  168.       bfd *abfd AND
  169.       struct external_exec *raw_bytes AND
  170.       struct internal_exec *execp)
  171. {
  172.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  173.  
  174.   /* Now fill in fields in the execp, from the bytes in the raw data.  */
  175.   execp->a_info   = bfd_h_get_32 (abfd, bytes->e_info);
  176.   execp->a_text   = GET_WORD (abfd, bytes->e_text);
  177.   execp->a_data   = GET_WORD (abfd, bytes->e_data);
  178.   execp->a_bss    = GET_WORD (abfd, bytes->e_bss);
  179.   execp->a_syms   = GET_WORD (abfd, bytes->e_syms);
  180.   execp->a_entry  = GET_WORD (abfd, bytes->e_entry);
  181.   execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
  182.   execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
  183. }
  184.  
  185. /*doc*
  186. *i aout_<size>_swap_exec_header_out
  187. Swaps the information in an internal exec header structure into the
  188. supplied buffer ready for writing to disk.
  189. *; PROTO(void, aout_<size>_swap_exec_header_out,
  190.       (bfd *abfd,
  191.        struct internal_exec *execp,
  192.        struct external_exec *raw_bytes));
  193. */
  194. void
  195. DEFUN(NAME(aout,swap_exec_header_out),(abfd, execp, raw_bytes),
  196.      bfd *abfd AND
  197.      struct internal_exec *execp AND 
  198.      struct external_exec *raw_bytes)
  199. {
  200.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  201.  
  202.   /* Now fill in fields in the raw data, from the fields in the exec struct. */
  203.   bfd_h_put_32 (abfd, execp->a_info  , bytes->e_info);
  204.   PUT_WORD (abfd, execp->a_text  , bytes->e_text);
  205.   PUT_WORD (abfd, execp->a_data  , bytes->e_data);
  206.   PUT_WORD (abfd, execp->a_bss   , bytes->e_bss);
  207.   PUT_WORD (abfd, execp->a_syms  , bytes->e_syms);
  208.   PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
  209.   PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
  210.   PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
  211. }
  212.  
  213. struct container {
  214.     struct aoutdata a;
  215.     struct internal_exec e;
  216. };
  217.  
  218.  
  219. /*doc*
  220. *i aout_<size>_some_aout_object_p
  221.  
  222. Some A.OUT variant thinks that the file whose format we're checking
  223. is an a.out file.  Do some more checking, and set up for access if
  224. it really is.  Call back to the calling environments "finish up"
  225. function just before returning, to handle any last-minute setup.  
  226.  
  227. *; PROTO(bfd_target *, aout_<size>_some_aout_object_p,
  228.      (bfd *abfd,
  229.       bfd_target *(*callback_to_real_object_p)()));
  230. */
  231.  
  232. bfd_target *
  233. DEFUN(NAME(aout,some_aout_object_p),(abfd, callback_to_real_object_p),
  234.       bfd *abfd AND
  235.       bfd_target *(*callback_to_real_object_p) ())
  236. {
  237.   struct external_exec exec_bytes;
  238.   struct internal_exec *execp;
  239.   struct container *rawptr;
  240.  
  241.   if (bfd_seek (abfd, 0L, false) < 0) {
  242.     bfd_error = system_call_error;
  243.     return 0;
  244.   }
  245.  
  246.   if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
  247.       != EXEC_BYTES_SIZE) {
  248.     bfd_error = wrong_format;
  249.     return 0;
  250.   }
  251.  
  252.   /* Use an intermediate variable for clarity */
  253.   rawptr = (struct container *) bfd_zalloc (abfd, sizeof (struct container));
  254.  
  255.   if (rawptr == NULL) {
  256.     bfd_error = no_memory;
  257.     return 0;
  258.   }
  259.  
  260.   set_tdata (abfd, rawptr);
  261.   exec_hdr (abfd) = execp = &(rawptr->e);
  262.   NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, execp);
  263.  
  264.   /* Set the file flags */
  265.   abfd->flags = NO_FLAGS;
  266.   if (execp->a_drsize || execp->a_trsize)
  267.     abfd->flags |= HAS_RELOC;
  268.   if (execp->a_entry) 
  269.     abfd->flags |= EXEC_P;
  270.   if (execp->a_syms) 
  271.     abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
  272.  
  273.   if (N_MAGIC (*execp) == ZMAGIC) abfd->flags |= D_PAGED;
  274.   if (N_MAGIC (*execp) == NMAGIC) abfd->flags |= WP_TEXT;
  275.  
  276.   bfd_get_start_address (abfd) = execp->a_entry;
  277.  
  278.   obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
  279.   bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
  280.  
  281.   /* Set the default architecture and machine type.  These can be
  282.      overridden in the callback routine.  */
  283.   abfd->obj_arch = bfd_arch_unknown;
  284.   abfd->obj_machine = 0;
  285.  
  286.   /* The default relocation entry size is that of traditional V7 Unix.  */
  287.   obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  288.  
  289.   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
  290.      them */
  291.   obj_textsec (abfd) = (asection *)NULL;
  292.   obj_datasec (abfd) = (asection *)NULL;
  293.   obj_bsssec (abfd) = (asection *)NULL;
  294.   (void)bfd_make_section(abfd, ".text");
  295.   (void)bfd_make_section(abfd, ".data");
  296.   (void)bfd_make_section(abfd, ".bss");
  297.  
  298.   abfd->sections = obj_textsec (abfd);
  299.   obj_textsec (abfd)->next = obj_datasec (abfd);
  300.   obj_datasec (abfd)->next = obj_bsssec (abfd);
  301.  
  302.   obj_datasec (abfd)->size = execp->a_data;
  303.   obj_bsssec (abfd)->size = execp->a_bss;
  304.   obj_textsec (abfd)->size = execp->a_text;
  305.  
  306.   if (abfd->flags & D_PAGED) {
  307.     obj_textsec (abfd)->size -=  EXEC_BYTES_SIZE;
  308.   }
  309.     
  310.  
  311.   obj_textsec (abfd)->flags = (execp->a_trsize != 0 ?
  312.                                (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_HAS_CONTENTS) :
  313.                                (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS));
  314.   obj_datasec (abfd)->flags = (execp->a_drsize != 0 ?
  315.                                (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_HAS_CONTENTS) :
  316.                                (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS));
  317.   obj_bsssec (abfd)->flags = SEC_ALLOC;
  318.  
  319. #ifdef THIS_IS_ONLY_DOCUMENTATION
  320.   /* Call back to the format-dependent code to fill in the rest of the 
  321.      fields and do any further cleanup.  Things that should be filled
  322.      in by the callback:  */
  323.  
  324.   struct exec *execp = exec_hdr (abfd);
  325.  
  326.   /* The virtual memory addresses of the sections */
  327.   obj_datasec (abfd)->vma = N_DATADDR(*execp);
  328.   obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
  329.   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
  330.  
  331.   /* The file offsets of the sections */
  332.   obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
  333.   obj_datasec (abfd)->filepos = N_DATOFF(*execp);
  334.  
  335.   /* The file offsets of the relocation info */
  336.   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
  337.   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
  338.  
  339.   /* The file offsets of the string table and symbol table.  */
  340.   obj_str_filepos (abfd) = N_STROFF (*execp);
  341.   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  342.  
  343.   /* This common code can't fill in those things because they depend
  344.      on either the start address of the text segment, the rounding
  345.      up of virtual addersses between segments, or the starting file 
  346.      position of the text segment -- all of which varies among different
  347.      versions of a.out.  */
  348.  
  349.   /* Determine the architecture and machine type of the object file.  */
  350.   switch (N_MACHTYPE (*exec_hdr (abfd))) {
  351.   default:
  352.     abfd->obj_arch = bfd_arch_obscure;
  353.     break;
  354.   }
  355.  
  356.   /* Determine the size of a relocation entry */
  357.   switch (abfd->obj_arch) {
  358.   case bfd_arch_sparc:
  359.   case bfd_arch_a29k:
  360.     obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  361.   default:
  362.     obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  363.   }
  364.  
  365.   return abfd->xvec;
  366.  
  367.   /* The architecture is encoded in various ways in various a.out variants,
  368.      or is not encoded at all in some of them.  The relocation size depends
  369.      on the architecture and the a.out variant.  Finally, the return value
  370.      is the bfd_target vector in use.  If an error occurs, return zero and
  371.      set bfd_error to the appropriate error code.
  372.      
  373.      Formats such as b.out, which have additional fields in the a.out
  374.      header, should cope with them in this callback as well.  */
  375. #endif                /* DOCUMENTATION */
  376.  
  377.  
  378.   return (*callback_to_real_object_p)(abfd);
  379. }
  380.  
  381. /*doc*
  382. *i aout_<size>_mkobject
  383.  
  384. This routine initializes a bfd for use with a.out files.
  385.  
  386. *; PROTO(boolean, aout_<size>_mkobject, (bfd *));
  387. */
  388.  
  389. boolean
  390. DEFUN(NAME(aout,mkobject),(abfd),
  391.      bfd *abfd)
  392. {
  393.   struct container *rawptr;
  394.  
  395.   bfd_error = system_call_error;
  396.  
  397.   /* Use an intermediate variable for clarity */
  398.   rawptr = (struct container *)bfd_zalloc (abfd, sizeof (struct container));
  399.   
  400.   if (rawptr == NULL) {
  401.     bfd_error = no_memory;
  402.     return false;
  403.   }
  404.   
  405.   set_tdata (abfd, rawptr);
  406.   exec_hdr (abfd) = &(rawptr->e);
  407.   
  408.   /* For simplicity's sake we just make all the sections right here. */
  409.   
  410.   obj_textsec (abfd) = (asection *)NULL;
  411.   obj_datasec (abfd) = (asection *)NULL;
  412.   obj_bsssec (abfd) = (asection *)NULL;
  413.   bfd_make_section (abfd, ".text");
  414.   bfd_make_section (abfd, ".data");
  415.   bfd_make_section (abfd, ".bss");
  416.   
  417.   return true;
  418. }
  419.  
  420.  
  421. /*doc*
  422. *i aout_<size>_machine_type
  423.  
  424. Keep track of machine architecture and machine type for a.out's.
  425. Return the machine_type for a particular arch&machine, or M_UNKNOWN
  426. if that exact arch&machine can't be represented in a.out format.
  427.  
  428. If the architecture is understood, machine type 0 (default) should
  429. always be understood.  
  430.  
  431. *; PROTO(enum machine_type, aout_<size>_machine_type,
  432.      (enum bfd_architecture arch,
  433.       unsigned long machine));
  434. */
  435.  
  436. enum machine_type
  437. DEFUN(NAME(aout,machine_type),(arch, machine),
  438.       enum bfd_architecture arch AND
  439.       unsigned long machine)
  440. {
  441.   enum machine_type arch_flags;
  442.     
  443.   arch_flags = M_UNKNOWN;
  444.     
  445.   switch (arch) {
  446.   case bfd_arch_sparc:
  447.     if (machine == 0)    arch_flags = M_SPARC;
  448.     break;
  449.       
  450.   case bfd_arch_m68k:
  451.     switch (machine) {
  452.     case 0:        arch_flags = M_68010; break;
  453.     case 68000:        arch_flags = M_UNKNOWN;    break;
  454.     case 68010:        arch_flags = M_68010; break;
  455.     case 68020:        arch_flags = M_68020; break;
  456.     default:        arch_flags = M_UNKNOWN; break;
  457.     }
  458.     break;
  459.       
  460.   case bfd_arch_i386:
  461.     if (machine == 0)    arch_flags = M_386;
  462.     break;
  463.       
  464.   case bfd_arch_a29k:
  465.     if (machine == 0)    arch_flags = M_29K;
  466.     break;
  467.       
  468.   default:
  469.     arch_flags = M_UNKNOWN;
  470.     break;
  471.   }
  472.   return arch_flags;
  473. }
  474.  
  475. /*doc*
  476. *i aout_<size>_set_arch_mach
  477.  
  478. Sets the architecture and the machine of the bfd to those values
  479. supplied. Verifies that the format can support the architecture
  480. required.
  481.  
  482. *; PROTO(boolean, aout_<size>_set_arch_mach,
  483.      (bfd *,
  484.       enum bfd_architecture,
  485.       unsigned long machine));
  486. */
  487.  
  488. boolean
  489. DEFUN(NAME(aout,set_arch_mach),(abfd, arch, machine),
  490.       bfd *abfd AND
  491.       enum bfd_architecture arch AND
  492.       unsigned long machine)
  493. {
  494.   abfd->obj_arch = arch;
  495.   abfd->obj_machine = machine;
  496.   if (arch != bfd_arch_unknown &&
  497.       NAME(aout,machine_type) (arch, machine) == M_UNKNOWN)
  498.     return false;        /* We can't represent this type */
  499.   return true;            /* We're easy ... */
  500. }
  501.  
  502. /*doc*
  503. *i aout_<size>new_section_hook
  504.  
  505. Called by the bfd in response to a @code{bfd_make_section} request.
  506. *; PROTO(boolean, aout_<size>_new_section_hook,
  507.          (bfd *abfd,
  508.       asection *newsect));
  509. */
  510. boolean
  511. DEFUN(NAME(aout,new_section_hook),(abfd, newsect),
  512.       bfd *abfd AND
  513.       asection *newsect)
  514. {
  515.   /* align to double at least */
  516.   newsect->alignment_power = 3;
  517.     
  518.   if (bfd_get_format (abfd) == bfd_object) {
  519.     if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
  520.       obj_textsec(abfd)= newsect;
  521.       return true;
  522.     }
  523.       
  524.     if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
  525.       obj_datasec(abfd) = newsect;
  526.       return true;
  527.     }
  528.       
  529.     if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
  530.       obj_bsssec(abfd) = newsect;
  531.       return true;
  532.     }
  533.   }
  534.     
  535.   /* We allow more than three sections internally */
  536.   return true;
  537. }
  538.  
  539. boolean
  540. DEFUN(NAME(aout,set_section_contents),(abfd, section, location, offset, count),
  541.       bfd *abfd AND
  542.       sec_ptr section AND
  543.       PTR location AND
  544.       file_ptr offset AND
  545.       bfd_size_type count)
  546. {
  547.   if (abfd->output_has_begun == false)
  548.       {                /* set by bfd.c handler */
  549.     if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL)) 
  550.         {
  551.           bfd_error = invalid_operation;
  552.           return false;
  553.         }
  554. /*    if (abfd->flags & D_PAGED) {      
  555.       obj_textsec(abfd)->filepos = 0;
  556.     }
  557.     else*/ {
  558.       obj_textsec(abfd)->filepos = EXEC_BYTES_SIZE;
  559.     }
  560.     obj_textsec(abfd)->size = align_power(obj_textsec(abfd)->size,
  561.                           obj_textsec(abfd)->alignment_power);
  562.     obj_datasec(abfd)->filepos =  obj_textsec (abfd)->size + EXEC_BYTES_SIZE;
  563.     obj_datasec(abfd)->size = align_power(obj_datasec(abfd)->size,
  564.                           obj_datasec(abfd)->alignment_power);
  565.       
  566.       
  567.       }
  568.   /* regardless, once we know what we're doing, we might as well get going */
  569.   if (section != obj_bsssec(abfd)) 
  570.       {
  571.     bfd_seek (abfd, section->filepos + offset, SEEK_SET);
  572.       
  573.     if (count) {
  574.       return (bfd_write ((PTR)location, 1, count, abfd) == count) ?
  575.         true : false;
  576.     }
  577.     return false;
  578.       }
  579.   return true;
  580. }
  581.  
  582. /* Classify stabs symbols */
  583.  
  584. #define sym_in_text_section(sym) \
  585. (((sym)->type  & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
  586.  
  587. #define sym_in_data_section(sym) \
  588. (((sym)->type  & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
  589.  
  590. #define sym_in_bss_section(sym) \
  591. (((sym)->type  & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
  592.  
  593. /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
  594. zero in the "value" field.  Nonzeroes there are fortrancommon
  595. symbols.  */
  596. #define sym_is_undefined(sym) \
  597. ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
  598.  
  599. /* Symbol is a global definition if N_EXT is on and if it has
  600. a nonzero type field.  */
  601. #define sym_is_global_defn(sym) \
  602. (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
  603.  
  604. /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
  605. are on.  */
  606. #define sym_is_debugger_info(sym) \
  607. ((sym)->type & ~(N_EXT | N_TYPE))
  608.  
  609. #define sym_is_fortrancommon(sym)       \
  610. (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
  611.  
  612. /* Symbol is absolute if it has N_ABS set */
  613. #define sym_is_absolute(sym) \
  614. (((sym)->type  & N_TYPE)== N_ABS)
  615.  
  616.  
  617. #define sym_is_indirect(sym) \
  618. (((sym)->type & N_ABS)== N_ABS)
  619.  
  620. /* Only in their own functions for ease of debugging; when sym flags have
  621. stabilised these should be inlined into their (single) caller */
  622.  
  623. static void
  624. DEFUN(translate_from_native_sym_flags,(sym_pointer, cache_ptr, abfd),
  625.       struct external_nlist *sym_pointer AND
  626.       aout_symbol_type *cache_ptr AND
  627.       bfd *abfd)
  628. {
  629.   switch (cache_ptr->type & N_TYPE) {
  630.   case N_SETA:
  631.   case N_SETT:
  632.   case N_SETD:
  633.   case N_SETB:
  634.       {
  635.     char *copy = bfd_alloc(abfd, strlen(cache_ptr->symbol.name)+1);
  636.     asection *section ;
  637.     arelent_chain *reloc = (arelent_chain *)bfd_alloc(abfd, sizeof(arelent_chain));
  638.     strcpy(copy, cache_ptr->symbol.name);
  639.       section =  bfd_make_section(abfd,copy);
  640.     switch ( (cache_ptr->type  & N_TYPE) ) {
  641.     case N_SETA:
  642.       reloc->relent.section =  (asection *)NULL;
  643.       cache_ptr->symbol.section = (asection *)NULL;
  644.       break;
  645.     case N_SETT:
  646.       reloc->relent.section = (asection *)obj_textsec(abfd);
  647.       cache_ptr->symbol.value -= reloc->relent.section->vma;
  648.       break;
  649.     case N_SETD:
  650.       reloc->relent.section = (asection *)obj_datasec(abfd);
  651.       cache_ptr->symbol.value -= reloc->relent.section->vma;
  652.       break;
  653.     case N_SETB:
  654.       reloc->relent.section = (asection *)obj_bsssec(abfd);
  655.       cache_ptr->symbol.value -= reloc->relent.section->vma;
  656.       break;
  657.     }
  658.     cache_ptr->symbol.section = reloc->relent.section;
  659.     reloc->relent.addend = cache_ptr->symbol.value ;
  660.       
  661.     /* We modify the symbol to belong to a section depending upon the
  662.        name of the symbol - probably __CTOR__ or __DTOR__ but we don't
  663.        really care, and add to the size of the section to contain a
  664.        pointer to the symbol. Build a reloc entry to relocate to this
  665.        symbol attached to this section.  */
  666.       
  667.     section->flags = SEC_CONSTRUCTOR;
  668.     section->reloc_count++;
  669.     section->alignment_power = 2;
  670.     reloc->relent.sym_ptr_ptr = (asymbol **)NULL;
  671.     reloc->next = section->constructor_chain;
  672.     section->constructor_chain = reloc;
  673.     reloc->relent.address = section->size;
  674.     section->size += sizeof(int *);
  675.       
  676.     reloc->relent.howto = howto_table_ext +CTOR_TABLE_RELOC_IDX;
  677.     cache_ptr->symbol.flags |=  BSF_DEBUGGING ;
  678.       }
  679.     break;
  680.   default:
  681.     
  682.     if (sym_is_debugger_info (cache_ptr)) {
  683.       cache_ptr->symbol.flags = BSF_DEBUGGING ;
  684.       /* Work out the section correct for this symbol */
  685.       switch (cache_ptr->type & N_TYPE) 
  686.       {
  687.       case N_TEXT:
  688.       case N_FN:
  689.         cache_ptr->symbol.section = obj_textsec (abfd);
  690.         cache_ptr->symbol.value -= obj_textsec(abfd)->vma;
  691.         break;
  692.       case N_DATA:
  693.         cache_ptr->symbol.value  -= obj_datasec(abfd)->vma;
  694.         cache_ptr->symbol.section = obj_datasec (abfd);
  695.         break;
  696.       case N_BSS :
  697.         cache_ptr->symbol.section = obj_bsssec (abfd);
  698.         cache_ptr->symbol.value -= obj_bsssec(abfd)->vma;
  699.         break;
  700.       case N_ABS:
  701.       default:
  702.         cache_ptr->symbol.section = 0;
  703.         break;
  704.       }
  705.     }
  706.     else {
  707.       if (sym_is_fortrancommon (cache_ptr))
  708.       {
  709.         cache_ptr->symbol.flags = BSF_FORT_COMM;
  710.         cache_ptr->symbol.section = (asection *)NULL;
  711.       }
  712.       else {
  713.     if (sym_is_undefined (cache_ptr)) {
  714.       cache_ptr->symbol.flags = BSF_UNDEFINED;
  715.     }
  716.     else if (sym_is_global_defn (cache_ptr)) {
  717.       cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
  718.     }
  719.  
  720.     else if (sym_is_absolute (cache_ptr)) {
  721.       cache_ptr->symbol.flags = BSF_ABSOLUTE;
  722.     }
  723.     else {
  724.       cache_ptr->symbol.flags = BSF_LOCAL;
  725.     }
  726.  
  727.     /* In a.out, the value of a symbol is always relative to the 
  728.      * start of the file, if this is a data symbol we'll subtract
  729.      * the size of the text section to get the section relative
  730.      * value. If this is a bss symbol (which would be strange)
  731.      * we'll subtract the size of the previous two sections
  732.      * to find the section relative address.
  733.      */
  734.  
  735.     if (sym_in_text_section (cache_ptr))   {
  736.       cache_ptr->symbol.value -= obj_textsec(abfd)->vma;
  737.       cache_ptr->symbol.section = obj_textsec (abfd);
  738.     }
  739.     else if (sym_in_data_section (cache_ptr)){
  740.       cache_ptr->symbol.value -= obj_datasec(abfd)->vma;
  741.       cache_ptr->symbol.section = obj_datasec (abfd);
  742.     }
  743.     else if (sym_in_bss_section(cache_ptr)) {
  744.       cache_ptr->symbol.section = obj_bsssec (abfd);
  745.       cache_ptr->symbol.value -= obj_bsssec(abfd)->vma;
  746.     }
  747.     else {
  748.       cache_ptr->symbol.section = (asection *)NULL;
  749.       cache_ptr->symbol.flags |= BSF_ABSOLUTE;
  750.     }
  751.       }
  752.     }
  753.   }
  754. }
  755.  
  756. static void
  757. DEFUN(translate_to_native_sym_flags,(sym_pointer, cache_ptr, abfd),
  758.      struct external_nlist *sym_pointer AND
  759.      asymbol *cache_ptr AND
  760.      bfd *abfd)
  761. {
  762.   bfd_vma value = cache_ptr->value;
  763.  
  764.   if (bfd_get_section(cache_ptr)) {
  765.     if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) {
  766.       sym_pointer->e_type[0] |= N_BSS;
  767.     }
  768.     else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) {
  769.       sym_pointer->e_type[0] |= N_DATA;
  770.     }
  771.     else  if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) {
  772.       sym_pointer->e_type[0] |= N_TEXT;
  773.     }
  774.     else {
  775.       bfd_error_vector.nonrepresentable_section(abfd,
  776.                         bfd_get_output_section(cache_ptr)->name);
  777.     }
  778.     /* Turn the symbol from section relative to absolute again */
  779.     
  780.     value +=
  781.       cache_ptr->section->output_section->vma 
  782.     + cache_ptr->section->output_offset ;
  783.   }
  784.   else {
  785.     sym_pointer->e_type[0] |= N_ABS;
  786.   }
  787.   
  788.   if (cache_ptr->flags & (BSF_FORT_COMM | BSF_UNDEFINED)) {
  789.     sym_pointer->e_type[0] = (N_UNDF | N_EXT);
  790.   }
  791.   else {
  792.     if (cache_ptr->flags & BSF_ABSOLUTE) {
  793.       sym_pointer->e_type[0] |= N_ABS;
  794.     }
  795.     
  796.     if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) {
  797.       sym_pointer->e_type[0] |= N_EXT;
  798.     }
  799.     if (cache_ptr->flags & BSF_DEBUGGING) {
  800.       sym_pointer->e_type [0]= ((aout_symbol_type *)cache_ptr)->type;
  801.     }
  802.   }
  803.   PUT_WORD(abfd, value, sym_pointer->e_value);
  804. }
  805.  
  806. /* Native-level interface to symbols. */
  807.  
  808. /* We read the symbols into a buffer, which is discarded when this
  809. function exits.  We read the strings into a buffer large enough to
  810. hold them all plus all the cached symbol entries. */
  811.  
  812. asymbol *
  813. DEFUN(NAME(aout,make_empty_symbol),(abfd),
  814.       bfd *abfd)
  815.   {
  816.     aout_symbol_type  *new =
  817.       (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
  818.     new->symbol.the_bfd = abfd;
  819.     
  820.     return &new->symbol;
  821.   }
  822.  
  823. boolean
  824. DEFUN(NAME(aout,slurp_symbol_table),(abfd),
  825.       bfd *abfd)
  826.   {
  827.     bfd_size_type symbol_size;
  828.     bfd_size_type string_size;
  829.     unsigned char string_chars[BYTES_IN_WORD];
  830.     struct external_nlist *syms;
  831.     char *strings;
  832.     aout_symbol_type *cached;
  833.     
  834.     /* If there's no work to be done, don't do any */
  835.     if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true;
  836.     symbol_size = exec_hdr(abfd)->a_syms;
  837.     if (symbol_size == 0) {
  838.       bfd_error = no_symbols;
  839.       return false;
  840.     }
  841.     
  842.     bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
  843.     if (bfd_read ((PTR)string_chars, BYTES_IN_WORD, 1, abfd) != BYTES_IN_WORD)
  844.       return false;
  845.     string_size = GET_WORD (abfd, string_chars);
  846.     
  847.     strings =(char *) bfd_alloc(abfd, string_size + 1);
  848.     cached = (aout_symbol_type *)
  849.       bfd_zalloc(abfd, (bfd_size_type)(bfd_get_symcount (abfd) * sizeof(aout_symbol_type)));
  850.  
  851.     /* malloc this, so we can free it if simply. The symbol caching
  852.        might want to allocate onto the bfd's obstack  */
  853.     syms = (struct external_nlist *) malloc(symbol_size);
  854.     bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
  855.     if (bfd_read ((PTR)syms, 1, symbol_size, abfd) != symbol_size) {
  856.     bailout:
  857.       if (syms)     free (syms);
  858.       if (cached)    bfd_release (abfd, cached);
  859.       if (strings)bfd_release (abfd, strings);
  860.       return false;
  861.     }
  862.     
  863.     bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
  864.     if (bfd_read ((PTR)strings, 1, string_size, abfd) != string_size) {
  865.       goto bailout;
  866.     }
  867.     
  868.     /* OK, now walk the new symtable, cacheing symbol properties */
  869.       {
  870.     register struct external_nlist *sym_pointer;
  871.     register struct external_nlist *sym_end = syms + bfd_get_symcount (abfd);
  872.     register aout_symbol_type *cache_ptr = cached;
  873.     
  874.     /* Run through table and copy values */
  875.     for (sym_pointer = syms, cache_ptr = cached;
  876.          sym_pointer < sym_end; sym_pointer++, cache_ptr++) 
  877.         {
  878.           bfd_vma x = GET_WORD(abfd, sym_pointer->e_strx);
  879.           cache_ptr->symbol.the_bfd = abfd;
  880.           if (x)
  881.         cache_ptr->symbol.name = x + strings;
  882.           else
  883.         cache_ptr->symbol.name = (char *)NULL;
  884.           
  885.           cache_ptr->symbol.value = GET_SWORD(abfd,  sym_pointer->e_value);
  886.           cache_ptr->desc = bfd_get_16(abfd, sym_pointer->e_desc);
  887.           cache_ptr->other =bfd_get_8(abfd, sym_pointer->e_other);
  888.           cache_ptr->type = bfd_get_8(abfd,  sym_pointer->e_type);
  889.           cache_ptr->symbol.udata = 0;
  890.           translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd);
  891.         }
  892.       }
  893.     
  894.     obj_aout_symbols (abfd) =  cached;
  895.     free((PTR)syms);
  896.     
  897.     return true;
  898.   }
  899.  
  900.  
  901. void
  902. DEFUN(NAME(aout,write_syms),(abfd),
  903.       bfd *abfd)
  904.   {
  905.     unsigned int count ;
  906.     asymbol **generic = bfd_get_outsymbols (abfd);
  907.     
  908.     bfd_size_type stindex = BYTES_IN_WORD; /* initial string length */
  909.     
  910.     for (count = 0; count < bfd_get_symcount (abfd); count++) {
  911.       asymbol *g = generic[count];
  912.       struct external_nlist nsp;
  913.       
  914.       
  915.       if (g->name) {
  916.     unsigned int length = strlen(g->name) +1;
  917.     PUT_WORD  (abfd, stindex, (unsigned char *)nsp.e_strx);
  918.     stindex += length;
  919.       }
  920.       else {
  921.     PUT_WORD  (abfd, 0, (unsigned char *)nsp.e_strx);
  922.       }
  923.       
  924.       if (g->the_bfd->xvec->flavour == abfd->xvec->flavour) 
  925.       {
  926.         bfd_h_put_16(abfd, aout_symbol(g)->desc,  nsp.e_desc);
  927.         bfd_h_put_8(abfd, aout_symbol(g)->other,  nsp.e_other);
  928.         bfd_h_put_8(abfd, aout_symbol(g)->type,  nsp.e_type);
  929.       }
  930.       else
  931.       {
  932.         bfd_h_put_16(abfd,0, nsp.e_desc);
  933.         bfd_h_put_8(abfd, 0,  nsp.e_other);
  934.         bfd_h_put_8(abfd, 0,  nsp.e_type);
  935.       }
  936.       
  937.       
  938.       
  939.       translate_to_native_sym_flags (&nsp, (PTR)g, abfd);
  940.       
  941.       bfd_write((PTR)&nsp,1,EXTERNAL_LIST_SIZE, abfd);
  942.     }
  943.     
  944.     
  945.     /* Now output the strings.  Be sure to put string length into correct
  946.       * byte ordering before writing it.
  947.     */
  948.       {
  949.     char buffer[BYTES_IN_WORD];
  950.     PUT_WORD  (abfd, stindex, (unsigned char *)buffer);
  951.     
  952.     bfd_write((PTR)buffer, 1, BYTES_IN_WORD, abfd);
  953.       }
  954.     generic = bfd_get_outsymbols(abfd);
  955.     for (count = 0; count < bfd_get_symcount(abfd); count++) 
  956.     {
  957.       asymbol *g = *(generic++);
  958.       
  959.       if (g->name)
  960.           {
  961.         size_t length = strlen(g->name)+1;
  962.         bfd_write((PTR)g->name, 1, length, abfd);
  963.           }
  964.       if ((g->flags & BSF_FAKE)==0) {
  965.         g->name = itos(count);    /* smash the generic symbol */
  966.       }
  967.     }
  968.   }
  969.  
  970.  
  971.  
  972. unsigned int
  973. DEFUN(NAME(aout,get_symtab),(abfd, location),
  974.       bfd *abfd AND
  975.       asymbol **location)
  976.   {
  977.     unsigned int counter = 0;
  978.     aout_symbol_type *symbase;
  979.     
  980.     if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
  981.     
  982.     for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
  983.       *(location++) = (asymbol *)( symbase++);
  984.     *location++ =0;
  985.     return bfd_get_symcount(abfd);
  986.   }
  987.  
  988.  
  989. /* Standard reloc stuff */
  990. /* Output standard relocation information to a file in target byte order. */
  991.  
  992. void
  993. DEFUN(NAME(aout,swap_std_reloc_out),(abfd, g, natptr),
  994.       bfd *abfd AND
  995.       arelent *g AND
  996.       struct reloc_std_external *natptr)
  997.   {
  998.     int r_index;
  999.     int r_extern;
  1000.     unsigned int r_length;
  1001.     int r_pcrel;
  1002.     int r_baserel, r_jmptable, r_relative;
  1003.     unsigned int r_addend;
  1004.     
  1005.     PUT_WORD(abfd, g->address, natptr->r_address);
  1006.     
  1007.     r_length = g->howto->size ; /* Size as a power of two */
  1008.     r_pcrel  = (int) g->howto->pc_relative; /* Relative to PC? */
  1009.     /* r_baserel, r_jmptable, r_relative???  FIXME-soon */
  1010.     r_baserel = 0;
  1011.     r_jmptable = 0;
  1012.     r_relative = 0;
  1013.     
  1014.     r_addend = g->addend;    /* Start here, see how it goes */
  1015.     
  1016.     /* name was clobbered by aout_write_syms to be symbol index */
  1017.     
  1018.     if (g->sym_ptr_ptr != NULL) 
  1019.     {
  1020.       if ((*(g->sym_ptr_ptr))->section) {
  1021.         /* put the section offset into the addend for output */
  1022.         r_addend += (*(g->sym_ptr_ptr))->section->vma;
  1023.       }
  1024.       
  1025.       r_index = stoi((*(g->sym_ptr_ptr))->name);
  1026.       r_extern = 1;
  1027.     }
  1028.     else {
  1029.       r_extern = 0;
  1030.       if (g->section == NULL) {
  1031.     /* It is possible to have a reloc with nothing, we generate an
  1032.       abs + 0 */
  1033.     r_addend = 0;
  1034.     r_index = N_ABS | N_EXT;
  1035.       }
  1036.       else  if(g->section->output_section == obj_textsec(abfd)) {
  1037.     r_index = N_TEXT | N_EXT;
  1038.     r_addend += g->section->output_section->vma;
  1039.       }
  1040.       else if (g->section->output_section == obj_datasec(abfd)) {
  1041.     r_index = N_DATA | N_EXT;
  1042.     r_addend += g->section->output_section->vma;
  1043.       }
  1044.       else if (g->section->output_section == obj_bsssec(abfd)) {
  1045.     r_index = N_BSS | N_EXT ;
  1046.     r_addend += g->section->output_section->vma;
  1047.       }
  1048.       else {
  1049.     BFD_ASSERT(0);
  1050.       }
  1051.     }
  1052.     
  1053.     /* now the fun stuff */
  1054.     if (abfd->xvec->header_byteorder_big_p != false) {
  1055.       natptr->r_index[0] = r_index >> 16;
  1056.       natptr->r_index[1] = r_index >> 8;
  1057.       natptr->r_index[2] = r_index;
  1058.       natptr->r_type[0] =
  1059.     (r_extern?    RELOC_STD_BITS_EXTERN_BIG: 0)
  1060.       | (r_pcrel?     RELOC_STD_BITS_PCREL_BIG: 0)
  1061.         | (r_baserel?   RELOC_STD_BITS_BASEREL_BIG: 0)
  1062.           | (r_jmptable?  RELOC_STD_BITS_JMPTABLE_BIG: 0)
  1063.         | (r_relative?  RELOC_STD_BITS_RELATIVE_BIG: 0)
  1064.           | (r_length <<  RELOC_STD_BITS_LENGTH_SH_BIG);
  1065.     } else {
  1066.       natptr->r_index[2] = r_index >> 16;
  1067.       natptr->r_index[1] = r_index >> 8;
  1068.       natptr->r_index[0] = r_index;
  1069.       natptr->r_type[0] =
  1070.     (r_extern?    RELOC_STD_BITS_EXTERN_LITTLE: 0)
  1071.       | (r_pcrel?     RELOC_STD_BITS_PCREL_LITTLE: 0)
  1072.         | (r_baserel?   RELOC_STD_BITS_BASEREL_LITTLE: 0)
  1073.           | (r_jmptable?  RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
  1074.         | (r_relative?  RELOC_STD_BITS_RELATIVE_LITTLE: 0)
  1075.           | (r_length <<  RELOC_STD_BITS_LENGTH_SH_LITTLE);
  1076.     }
  1077.   }
  1078.  
  1079.  
  1080. /* Extended stuff */
  1081. /* Output extended relocation information to a file in target byte order. */
  1082.  
  1083. void
  1084. DEFUN(NAME(aout,swap_ext_reloc_out),(abfd, g, natptr),
  1085.       bfd *abfd AND
  1086.       arelent *g AND
  1087.       register struct reloc_ext_external *natptr)
  1088.   {
  1089.     int r_index;
  1090.     int r_extern;
  1091.     unsigned int r_type;
  1092.     unsigned int r_addend;
  1093.     
  1094.     PUT_WORD (abfd, g->address, natptr->r_address);
  1095.     
  1096.     /* Find a type in the output format which matches the input howto - 
  1097.       at the moment we assume input format == output format FIXME!! */
  1098.     r_type = (enum reloc_type) g->howto->type;
  1099.     
  1100.     r_addend = g->addend;    /* Start here, see how it goes */
  1101.  
  1102.   /* name was clobbered by aout_write_syms to be symbol index*/
  1103.  
  1104.   if (g->sym_ptr_ptr != NULL) 
  1105.     {
  1106.       if ((*(g->sym_ptr_ptr))->section) {
  1107.     /* put the section offset into the addend for output */
  1108.     r_addend += (*(g->sym_ptr_ptr))->section->vma;
  1109.       }
  1110.  
  1111.       r_index = stoi((*(g->sym_ptr_ptr))->name);
  1112.       r_extern = 1;
  1113.     }
  1114.   else {
  1115.     r_extern = 0;
  1116.     if (g->section == NULL) {
  1117.       BFD_ASSERT(0);
  1118.       r_index = N_ABS | N_EXT;
  1119.     }
  1120.     else  if(g->section->output_section == obj_textsec(abfd)) {
  1121.       r_index = N_TEXT | N_EXT;
  1122.       r_addend += g->section->output_section->vma;
  1123.     }
  1124.     else if (g->section->output_section == obj_datasec(abfd)) {
  1125.       r_index = N_DATA | N_EXT;
  1126.       r_addend += g->section->output_section->vma;
  1127.     }
  1128.     else if (g->section->output_section == obj_bsssec(abfd)) {
  1129.       r_index = N_BSS | N_EXT ;
  1130.       r_addend += g->section->output_section->vma;
  1131.     }
  1132.     else {
  1133.       BFD_ASSERT(0);
  1134.     }
  1135.   }
  1136.  
  1137.   /* now the fun stuff */
  1138.   if (abfd->xvec->header_byteorder_big_p != false) {
  1139.     natptr->r_index[0] = r_index >> 16;
  1140.     natptr->r_index[1] = r_index >> 8;
  1141.     natptr->r_index[2] = r_index;
  1142.     natptr->r_type[0] =
  1143.       (r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
  1144.     | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG);
  1145.   } else {
  1146.     natptr->r_index[2] = r_index >> 16;
  1147.     natptr->r_index[1] = r_index >> 8;
  1148.     natptr->r_index[0] = r_index;
  1149.     natptr->r_type[0] =
  1150.       (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
  1151.     | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
  1152.   }
  1153.  
  1154.   PUT_WORD (abfd, r_addend, natptr->r_addend);
  1155. }
  1156.  
  1157. #define MOVE_ADDRESS(ad)                               \
  1158.   if (r_extern) {                            \
  1159.     cache_ptr->sym_ptr_ptr = symbols + r_index;                \
  1160.     cache_ptr->section = (asection *)NULL;                \
  1161.       cache_ptr->addend = ad;                        \
  1162.   } else {                                \
  1163.     cache_ptr->sym_ptr_ptr = (asymbol **)NULL;                \
  1164.     switch (r_index) {                            \
  1165.     case N_TEXT:                            \
  1166.     case N_TEXT | N_EXT:                        \
  1167.       cache_ptr->section = obj_textsec(abfd);                \
  1168.       cache_ptr->addend = ad  - su->textsec->vma;            \
  1169.       break;                                \
  1170.     case N_DATA:                            \
  1171.     case N_DATA | N_EXT:                        \
  1172.       cache_ptr->section = obj_datasec(abfd);                \
  1173.       cache_ptr->addend = ad - su->datasec->vma;            \
  1174.       break;                                \
  1175.     case N_BSS:                                \
  1176.     case N_BSS | N_EXT:                            \
  1177.       cache_ptr->section = obj_bsssec(abfd);                \
  1178.       cache_ptr->addend = ad - su->bsssec->vma;                \
  1179.       break;                                \
  1180.     case N_ABS:                                \
  1181.     case N_ABS | N_EXT:                            \
  1182.       cache_ptr->section = NULL;    /* No section */        \
  1183.       cache_ptr->addend = ad;        /* FIXME, is this right? */    \
  1184.       BFD_ASSERT(1);                            \
  1185.       break;                                \
  1186.     default:                                \
  1187.       cache_ptr->section = NULL;    /* No section */        \
  1188.       cache_ptr->addend = ad;        /* FIXME, is this right? */    \
  1189.       BFD_ASSERT(1);                            \
  1190.       break;                                \
  1191.     }                                    \
  1192.   }                                     \
  1193.  
  1194. void
  1195. DEFUN(NAME(aout,swap_ext_reloc_in), (abfd, bytes, cache_ptr, symbols),
  1196.       bfd *abfd AND
  1197.       struct reloc_ext_external *bytes AND
  1198.       arelent *cache_ptr AND
  1199.       asymbol **symbols)
  1200. {
  1201.   int r_index;
  1202.   int r_extern;
  1203.   unsigned int r_type;
  1204.   struct aoutdata *su = (struct aoutdata *)(abfd->tdata);
  1205.  
  1206.   cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
  1207.  
  1208.   /* now the fun stuff */
  1209.   if (abfd->xvec->header_byteorder_big_p != false) {
  1210.     r_index =  (bytes->r_index[0] << 16)
  1211.          | (bytes->r_index[1] << 8)
  1212.          |  bytes->r_index[2];
  1213.     r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
  1214.     r_type   =       (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
  1215.                       >> RELOC_EXT_BITS_TYPE_SH_BIG;
  1216.   } else {
  1217.     r_index =  (bytes->r_index[2] << 16)
  1218.          | (bytes->r_index[1] << 8)
  1219.          |  bytes->r_index[0];
  1220.     r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
  1221.     r_type   =       (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
  1222.                       >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
  1223.   }
  1224.  
  1225.   cache_ptr->howto =  howto_table_ext + r_type;
  1226.   MOVE_ADDRESS(GET_SWORD(abfd,bytes->r_addend));
  1227. }
  1228.  
  1229. void
  1230. DEFUN(NAME(aout,swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols),
  1231.   bfd *abfd AND
  1232.   struct reloc_std_external *bytes AND
  1233.   arelent *cache_ptr AND
  1234.   asymbol **symbols)
  1235. {
  1236.   int r_index;
  1237.   int r_extern;
  1238.   unsigned int r_length;
  1239.   int r_pcrel;
  1240.   int r_baserel, r_jmptable, r_relative;
  1241.   struct aoutdata *su = (struct aoutdata *)(abfd->tdata);
  1242.  
  1243.   cache_ptr->address = (int32_type)(bfd_h_get_32 (abfd, bytes->r_address));
  1244.  
  1245.   /* now the fun stuff */
  1246.   if (abfd->xvec->header_byteorder_big_p != false) {
  1247.     r_index =  (bytes->r_index[0] << 16)
  1248.       | (bytes->r_index[1] << 8)
  1249.     |  bytes->r_index[2];
  1250.     r_extern  = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
  1251.     r_pcrel   = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
  1252.     r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
  1253.     r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
  1254.     r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
  1255.     r_length  =       (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG) 
  1256.                   >> RELOC_STD_BITS_LENGTH_SH_BIG;
  1257.   } else {
  1258.     r_index =  (bytes->r_index[2] << 16)
  1259.       | (bytes->r_index[1] << 8)
  1260.     |  bytes->r_index[0];
  1261.     r_extern  = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  1262.     r_pcrel   = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  1263.     r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
  1264.     r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
  1265.     r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
  1266.     r_length  =       (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) 
  1267.                   >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
  1268.   }
  1269.  
  1270.   cache_ptr->howto =  howto_table_std + r_length + 4 * r_pcrel;
  1271.   /* FIXME-soon:  Roll baserel, jmptable, relative bits into howto setting */
  1272.  
  1273.   MOVE_ADDRESS(0);
  1274. }
  1275.  
  1276. /* Reloc hackery */
  1277.  
  1278. boolean
  1279. DEFUN(NAME(aout,slurp_reloc_table),(abfd, asect, symbols),
  1280.       bfd *abfd AND
  1281.       sec_ptr asect AND
  1282.       asymbol **symbols)
  1283. {
  1284.   unsigned int count;
  1285.   bfd_size_type reloc_size;
  1286.   PTR relocs;
  1287.   arelent *reloc_cache;
  1288.   size_t each_size;
  1289.  
  1290.   if (asect->relocation) return true;
  1291.  
  1292.   if (asect->flags & SEC_CONSTRUCTOR) return true;
  1293.  
  1294.   if (asect == obj_datasec (abfd)) {
  1295.     reloc_size = exec_hdr(abfd)->a_drsize;
  1296.     goto doit;
  1297.   }
  1298.  
  1299.   if (asect == obj_textsec (abfd)) {
  1300.     reloc_size = exec_hdr(abfd)->a_trsize;
  1301.     goto doit;
  1302.   }
  1303.  
  1304.   bfd_error = invalid_operation;
  1305.   return false;
  1306.  
  1307.  doit:
  1308.   bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
  1309.   each_size = obj_reloc_entry_size (abfd);
  1310.  
  1311.   count = reloc_size / each_size;
  1312.  
  1313.  
  1314.   reloc_cache = (arelent *) bfd_zalloc (abfd, (size_t)(count * sizeof
  1315.                                (arelent)));
  1316.   if (!reloc_cache) {
  1317. nomem:
  1318.     bfd_error = no_memory;
  1319.     return false;
  1320.   }
  1321.  
  1322.   relocs = (PTR) bfd_alloc (abfd, reloc_size);
  1323.   if (!relocs) {
  1324.     bfd_release (abfd, reloc_cache);
  1325.     goto nomem;
  1326.   }
  1327.  
  1328.   if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size) {
  1329.     bfd_release (abfd, relocs);
  1330.     bfd_release (abfd, reloc_cache);
  1331.     bfd_error = system_call_error;
  1332.     return false;
  1333.   }
  1334.  
  1335.   if (each_size == RELOC_EXT_SIZE) {
  1336.     register struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
  1337.     unsigned int counter = 0;
  1338.     arelent *cache_ptr = reloc_cache;
  1339.  
  1340.     for (; counter < count; counter++, rptr++, cache_ptr++) {
  1341.       NAME(aout,swap_ext_reloc_in)(abfd, rptr, cache_ptr, symbols);
  1342.     }
  1343.   } else {
  1344.     register struct reloc_std_external *rptr = (struct reloc_std_external*) relocs;
  1345.     unsigned int counter = 0;
  1346.     arelent *cache_ptr = reloc_cache;
  1347.  
  1348.     for (; counter < count; counter++, rptr++, cache_ptr++) {
  1349.     NAME(aout,swap_std_reloc_in)(abfd, rptr, cache_ptr, symbols);
  1350.     }
  1351.  
  1352.   }
  1353.  
  1354.   bfd_release (abfd,relocs);
  1355.   asect->relocation = reloc_cache;
  1356.   asect->reloc_count = count;
  1357.   return true;
  1358. }
  1359.  
  1360.  
  1361.  
  1362. /* Write out a relocation section into an object file.  */
  1363.  
  1364. boolean
  1365. DEFUN(NAME(aout,squirt_out_relocs),(abfd, section),
  1366.       bfd *abfd AND
  1367.       asection *section)
  1368. {
  1369.   arelent **generic;
  1370.   unsigned char *native, *natptr;
  1371.   size_t each_size;
  1372.  
  1373.   unsigned int count = section->reloc_count;
  1374.   size_t natsize;
  1375.  
  1376.   if (count == 0) return true;
  1377.  
  1378.   each_size = obj_reloc_entry_size (abfd);
  1379.   natsize = each_size * count;
  1380.   native = (unsigned char *) bfd_zalloc (abfd, natsize);
  1381.   if (!native) {
  1382.     bfd_error = no_memory;
  1383.     return false;
  1384.   }
  1385.  
  1386.   generic = section->orelocation;
  1387.  
  1388.   if (each_size == RELOC_EXT_SIZE) 
  1389.     {
  1390.       for (natptr = native;
  1391.        count != 0;
  1392.        --count, natptr += each_size, ++generic)
  1393.     NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
  1394.     }
  1395.   else 
  1396.     {
  1397.       for (natptr = native;
  1398.        count != 0;
  1399.        --count, natptr += each_size, ++generic)
  1400.     NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr);
  1401.     }
  1402.  
  1403.   if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
  1404.     bfd_release(abfd, native);
  1405.     return false;
  1406.   }
  1407.   bfd_release (abfd, native);
  1408.  
  1409.   return true;
  1410. }
  1411.  
  1412. /* This is stupid.  This function should be a boolean predicate */
  1413. unsigned int
  1414. DEFUN(NAME(aout,canonicalize_reloc),(abfd, section, relptr, symbols),
  1415.       bfd *abfd AND
  1416.       sec_ptr section AND
  1417.       arelent **relptr AND
  1418.       asymbol **symbols)
  1419. {
  1420.   arelent *tblptr = section->relocation;
  1421.   unsigned int count;
  1422.  
  1423.   if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
  1424.     return 0;
  1425.  
  1426.   if (section->flags & SEC_CONSTRUCTOR) {
  1427.     arelent_chain *chain = section->constructor_chain;
  1428.     for (count = 0; count < section->reloc_count; count ++) {
  1429.       *relptr ++ = &chain->relent;
  1430.       chain = chain->next;
  1431.     }
  1432.   }
  1433.   else {
  1434.     tblptr = section->relocation;
  1435.     if (!tblptr) return 0;
  1436.  
  1437.     for (count = 0; count++ < section->reloc_count;) 
  1438.       {
  1439.     *relptr++ = tblptr++;
  1440.       }
  1441.   }
  1442.   *relptr = 0;
  1443.  
  1444.   return section->reloc_count;
  1445. }
  1446.  
  1447. unsigned int
  1448. DEFUN(NAME(aout,get_reloc_upper_bound),(abfd, asect),
  1449.      bfd *abfd AND
  1450.      sec_ptr asect)
  1451. {
  1452.   if (bfd_get_format (abfd) != bfd_object) {
  1453.     bfd_error = invalid_operation;
  1454.     return 0;
  1455.   }
  1456.   if (asect->flags & SEC_CONSTRUCTOR) {
  1457.     return (sizeof (arelent *) * (asect->reloc_count+1));
  1458.   }
  1459.  
  1460.  
  1461.   if (asect == obj_datasec (abfd))
  1462.     return (sizeof (arelent *) *
  1463.             ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
  1464.              +1));
  1465.  
  1466.   if (asect == obj_textsec (abfd))
  1467.     return (sizeof (arelent *) *
  1468.             ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
  1469.              +1));
  1470.  
  1471.   bfd_error = invalid_operation;
  1472.   return 0;
  1473. }
  1474.  
  1475.  
  1476.  unsigned int
  1477. DEFUN(NAME(aout,get_symtab_upper_bound),(abfd),
  1478.      bfd *abfd)
  1479. {
  1480.   if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
  1481.  
  1482.   return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
  1483. }
  1484.  alent *
  1485. DEFUN(NAME(aout,get_lineno),(ignore_abfd, ignore_symbol),
  1486.       bfd *ignore_abfd AND
  1487.       asymbol *ignore_symbol)
  1488. {
  1489. return (alent *)NULL;
  1490. }
  1491.  
  1492.  
  1493. void 
  1494. DEFUN(NAME(aout,print_symbol),(ignore_abfd, afile, symbol, how),
  1495.       bfd *ignore_abfd AND
  1496.       PTR afile AND
  1497.       asymbol *symbol AND
  1498.       bfd_print_symbol_enum_type how)
  1499. {
  1500.   FILE *file = (FILE *)afile;
  1501.  
  1502.   switch (how) {
  1503.   case bfd_print_symbol_name_enum:
  1504.     fprintf(file,"%s", symbol->name);
  1505.     break;
  1506.   case bfd_print_symbol_type_enum:
  1507.     fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
  1508.         (unsigned)(aout_symbol(symbol)->other & 0xff),
  1509.         (unsigned)(aout_symbol(symbol)->type));
  1510.     break;
  1511.   case bfd_print_symbol_all_enum:
  1512.     {
  1513.    CONST char *section_name = symbol->section == (asection *)NULL ?
  1514.     "*abs" : symbol->section->name;
  1515.  
  1516.       bfd_print_symbol_vandf((PTR)file,symbol);
  1517.  
  1518.       fprintf(file," %-5s %04x %02x %02x %s",
  1519.           section_name,
  1520.           (unsigned)(aout_symbol(symbol)->desc & 0xffff),
  1521.           (unsigned)(aout_symbol(symbol)->other & 0xff),
  1522.           (unsigned)(aout_symbol(symbol)->type  & 0xff),
  1523.           symbol->name);
  1524.     }
  1525.     break;
  1526.   }
  1527. }
  1528.  
  1529. /* 
  1530.  provided a bfd, a section and an offset into the section, calculate
  1531.  and return the name of the source file and the line nearest to the
  1532.  wanted location.
  1533. */
  1534.  
  1535. boolean
  1536. DEFUN(NAME(aout,find_nearest_line),(abfd,
  1537.                      section,
  1538.                      symbols,
  1539.                      offset,
  1540.                      filename_ptr,
  1541.                      functionname_ptr,
  1542.                      line_ptr),
  1543.       bfd *abfd AND
  1544.       asection *section AND
  1545.       asymbol **symbols AND
  1546.       bfd_vma offset AND
  1547.       CONST char **filename_ptr AND
  1548.       CONST char **functionname_ptr AND
  1549.       unsigned int *line_ptr)
  1550. {
  1551.   /* Run down the file looking for the filename, function and linenumber */
  1552.   asymbol **p;
  1553.   static  char buffer[100];
  1554.   bfd_vma high_line_vma = ~0;
  1555.   bfd_vma low_func_vma = 0;
  1556.   asymbol *func = 0;
  1557.   *filename_ptr = abfd->filename;
  1558.   *functionname_ptr = 0;
  1559.   *line_ptr = 0;
  1560.   if (symbols != (asymbol **)NULL) {
  1561.     for (p = symbols; *p; p++) {
  1562.       aout_symbol_type  *q = (aout_symbol_type *)(*p);
  1563.       switch (q->type){
  1564.       case N_SO:
  1565.     *filename_ptr = q->symbol.name;
  1566.     if (obj_textsec(abfd) != section) {
  1567.       return true;
  1568.     }
  1569.     break;
  1570.       case N_SLINE:
  1571.  
  1572.       case N_DSLINE:
  1573.       case N_BSLINE:
  1574.     /* We'll keep this if it resolves nearer than the one we have already */
  1575.     if (q->symbol.value >= offset &&
  1576.         q->symbol.value < high_line_vma) {
  1577.       *line_ptr = q->desc;
  1578.       high_line_vma = q->symbol.value;
  1579.     }
  1580.     break;
  1581.       case N_FUN:
  1582.     {
  1583.       /* We'll keep this if it is nearer than the one we have already */
  1584.       if (q->symbol.value >= low_func_vma &&
  1585.           q->symbol.value <= offset) {
  1586.         low_func_vma = q->symbol.value;
  1587.         func = (asymbol *)q;
  1588.       }
  1589.       if (*line_ptr && func) {
  1590.         CONST char *function = func->name;
  1591.         char *p;
  1592.         strncpy(buffer, function, sizeof(buffer)-1);
  1593.         buffer[sizeof(buffer)-1] = 0;
  1594.         /* Have to remove : stuff */
  1595.         p = strchr(buffer,':');
  1596.         if (p != NULL) { *p = NULL; }
  1597.         *functionname_ptr = buffer;
  1598.         return true;
  1599.  
  1600.       }
  1601.     }
  1602.     break;
  1603.       }
  1604.     }
  1605.   }
  1606.   
  1607.   return true;
  1608.  
  1609. }
  1610.  
  1611. int 
  1612. DEFUN(NAME(aout,sizeof_headers),(ignore_abfd, execable),
  1613.       bfd *ignore_abfd AND
  1614.       boolean execable)
  1615. {
  1616.   return EXEC_BYTES_SIZE;
  1617. }
  1618.